//
//  EditHeadImgViewController.m
//  SouthChina
//
//  Created by kc on 15/7/6.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "EditHeadImgViewController.h"
#import "TakePhotoController.h"
#import "ImageShowerController.h"

@interface EditHeadImgViewController ()
@property (strong, nonatomic) IBOutlet UIButton *btImage; //头像按钮
@property (strong, nonatomic) IBOutlet UILabel *username;

@property (strong, nonatomic) UIImageView *  headImage;//头像
@property (strong, nonatomic) TakePhotoController * takephoto;
@property (strong, nonatomic) UIButton * finishBtn;//完成按钮
@end

@implementation EditHeadImgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addTitleBarWithBackBtn:@"头像修改" needReturn:YES];
    UIView * viewBody = [[[NSBundle mainBundle] loadNibNamed:@"EditHeadImgViewBody" owner:self options:nil] objectAtIndex:0];
    viewBody.frame = (CGRect){0, self.titlebar.frame.origin.y+self.titlebar.frame.size.height,SCREEN_WIDTH, SCREEN_HEIGHT-self.titlebar.frame.size.height};
    [self.view addSubview:viewBody];
    
    //用户头像
    NSString *headStr = [DE getGlobalVar:LOGIN_USER_HEADIMG] == nil?@"":[DE getGlobalVar:LOGIN_USER_HEADIMG];
    NSURL *url= [NSURL URLWithString: [headStr hasPrefix:@"http"]?headStr:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,headStr]];
    
    _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 65,65)];
    [_headImage setBackgroundColor:[UIColor clearColor]];
    [_headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    _headImage.layer.masksToBounds = YES;
    _headImage.layer.cornerRadius = 32;
     //按钮上增加图片
    [_btImage addSubview:_headImage];
    
    //登陆用户名
    NSString *strusrname = [DE getGlobalVar:LOGIN_USER_NAME] == nil?@"":[DE getGlobalVar:LOGIN_USER_NAME];
    NSLog(@"%@",strusrname);
    _username.text = strusrname;
    
    
    //标题栏上显示完成按钮
    //UIImage *img=[UIImage imageNamed:@"blue_btn_normal.png"];
   // _finishBtn = [[UIButton alloc] initWithFrame:CGRectMake(250, self.titlebar.frame.origin.y+32, 40, 20)];
   // [_finishBtn addTarget:self action:@selector(finishBtn:) forControlEvents:UIControlEventTouchUpInside];
  //  [_finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    //[_finishBtn setBackgroundImage:img forState:UIControlStateNormal];
    //[self.titlebar addSubview:_finishBtn];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


//点击头像事件，弹出头像列表
- (IBAction)btHeadImgClick:(id)sender {
    _takephoto = [TakePhotoController new];
    _takephoto.type = TakePhotoFromPhotoLibrary;
    _takephoto.isEdit = YES;
    __weak EditHeadImgViewController *ctl = self;
    _takephoto.blockComplete = ^(UIImage * image,NSDictionary*fileMap){
        
        [UploadFile uploadFileWithFilePath:[fileMap objectForKey:@"file"] servicePath:@"HEAD_IMG" params:fileMap calback:^(BOOL success, int code, NSString *desc, NSDictionary *netdata, NSDictionary *params) {
            
            if (success) {
                [DE setGlobalVar:LOGIN_USER_HEADIMG value:[netdata objectForKey:@"URL"]];
                NSLog(@"图像URL:::::::%@",[netdata objectForKey:@"URL"]);
                NSDictionary *params = @{@"user_id":DE.userId,@"headImg":[netdata objectForKey:@"URL"]};
                
                [MBProgressHUD showHUDAddedTo:ctl.view animated:YES];
                
                [DE serverCall:@"updateHeadImg" params:params callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
                    [MBProgressHUD hideHUDForView:ctl.view animated:YES];
                    if(success && [data objectForKey:@"result"])
                    {
                        [ctl.headImage setImage:image];
                    }
                }];
            }
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:success?@"更新头像成功!":@"更新头像失败！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            
        }];
        
    };
    
    [_takephoto startTakePhotoFromViewControl:kAppDelegate.iCSDrawerController];
}

-(void)finishBtn:(id)sender
{
    NSLog(@"finishBtn");
    NSString * linkStat = @"";
    
    NSDictionary *params = @{@"userName":_username.text,@"linkInfoState":linkStat};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [DE serverCall:@"updateUserInfo" params:params callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"提示" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success && [data objectForKey:@"result"])
        {
            [DE setGlobalVar:LOGIN_USER_NAME value:_username.text];
            [DE setGlobalVar:LINKINFOSTATE value:linkStat];
            view.message = @"信息提交成功！！";
            [view show];
            
        }else
        {
            view.message = @"信息提交失败！！";
            [view show];
        }
        
    }];
    [_username resignFirstResponder]; //点击完成按钮，键盘隐藏
}

- (void)btnBack {
    kAppDelegate.needRefrsh = YES;
    [super btnBack];
}

@end
